﻿using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Forms;

namespace PolePosition.v3.Desktop.Grids
{
    public partial class GridXMissionAsset : GridBase, IGridBase
    {
        public GridXMissionAsset()
            : base(Common.Enumerations.DataTypes.AssetsXmission)
        {
            InitializeComponent();
            advTreeAssets.Visible = false;
            contextMenuBarGrid.SetContextMenuEx(advTreeAssets, buttonItemContextMenu);
        }

        private bool m_CombosBound;
        private void BindCombos()
        {
            if (Program.LookupTableData != null && !m_CombosBound)
            {
                bindingSourcePoleSpecies.DataSource = Program.LookupTableData.XMissionStructuresPoleSpecies;
                comboBoxExPoleSpecies.DisplayMember = EntityLibrary.XMissionStructuresPoleSpeciesEnumerations.Columns.Name.ToString();
                comboBoxExPoleSpecies.ValueMember = EntityLibrary.XMissionStructuresPoleSpeciesEnumerations.Columns.XMissionStructures_PoleSpeciesId.ToString();
                comboBoxExPoleSpecies.DataSource = bindingSourcePoleSpecies;

                bindingSourcePoleClass.DataSource = Program.LookupTableData.XMissionStructuresPoleClasses;
                comboBoxExPoleClass.DisplayMember = EntityLibrary.XMissionStructuresPoleClassesEnumerations.Columns.Name.ToString();
                comboBoxExPoleClass.ValueMember = EntityLibrary.XMissionStructuresPoleClassesEnumerations.Columns.XMissionStructures_PoleClassId.ToString();
                comboBoxExPoleClass.DataSource = bindingSourcePoleClass;

                bindingSourcePoleTreatment.DataSource = Program.LookupTableData.XMissionStructuresPoleTreatments;
                comboBoxExPoleTreatment.DisplayMember = EntityLibrary.XMissionStructuresPoleTreatmentEnumerations.Columns.Name.ToString();
                comboBoxExPoleTreatment.ValueMember = EntityLibrary.XMissionStructuresPoleTreatmentEnumerations.Columns.XMissionStructures_PoleTreatmentId.ToString();
                comboBoxExPoleTreatment.DataSource = bindingSourcePoleTreatment;

                bindingSourcePoleTopType.DataSource = Program.LookupTableData.XMissionStructuresPoleTopTypes;
                comboBoxExPoleTopType.DisplayMember = EntityLibrary.XMissionStructuresPoleTopTypeEnumerations.Columns.Name.ToString();
                comboBoxExPoleTopType.ValueMember = EntityLibrary.XMissionStructuresPoleTopTypeEnumerations.Columns.XMissionStructures_PoleTopTypeId.ToString();
                comboBoxExPoleTopType.DataSource = bindingSourcePoleTopType;

                bindingSourceMap.DataSource = Program.LoginResult.Polygons;
                comboBoxExMap.DisplayMember = EntityLibrary.UtilityPolygonEnumerations.Columns.Name.ToString();
                comboBoxExMap.ValueMember = EntityLibrary.UtilityPolygonEnumerations.Columns.Utility_PolygonId.ToString();
                comboBoxExMap.DataSource = bindingSourceMap;

                m_CombosBound = true;
            }
        }

        #region Asynch Processing (Implements IGridBase)

        public override void DoAsynchLoad(DoWorkEventArgs e)
        {
            advTreeAssets.Invoke(new MethodInvoker(delegate { advTreeAssets.Enabled = false; }));

            int _TotalRecords;
            e.Result = ServiceProxy.UtilityService.GetXmissionAssetData(DataKeyId, PageSize, PageNumber, CurrentSearchType, CurrentSearchText, out _TotalRecords);
            TotalItems = _TotalRecords;
        }

        public override void AsychLoadComplete(object sender, RunWorkerCompletedEventArgs e)
        {
            base.AsychLoadComplete(sender, e);
            //do whatever needs to be done after asynch work complete (BackgroudWorker_RunWorkerCompleted)
            if (e.Cancelled)
            {

            }

            if (e.Error != null || e.Result == null)
            {
                //todo: create some sort of error dialog and use it here instead of messagebx
                DevComponents.DotNetBar.MessageBoxEx.Show("Error retrieving project data.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }


            List<EntityLibrary.UtilityAssetXMissionStructuresPoleEntity> _data;
            _data = e.Result as List<EntityLibrary.UtilityAssetXMissionStructuresPoleEntity>;

            if (_data == null || _data.Count() == 0)
            {
                if (CurrentSearchType == Common.Enumerations.GridSearchByTpes.None)
                    DevComponents.DotNetBar.MessageBoxEx.Show("No data found for the selected asset type.", "No Data Found", MessageBoxButtons.OK, MessageBoxIcon.Information);
                else
                    DevComponents.DotNetBar.MessageBoxEx.Show("No data found for the asset type with the applied search.", "No Data Found", MessageBoxButtons.OK, MessageBoxIcon.Information);

                return;
            }

            bindingSourceGridDataParent.DataSource = _data;
            advTreeAssets.DataSource = bindingSourceGridDataParent;
            advTreeAssets.Bind();
            advTreeAssets.Visible = true;
            advTreeAssets.Enabled = true;
        }

        public override void DoAsynchSave(DoWorkEventArgs e)
        {

        }

        public override void AsychSaveComplete(object sender, RunWorkerCompletedEventArgs e)
        {

        }

        public override void DoAsynchGetKml(DoWorkEventArgs e)
        {
            advTreeAssets.Invoke(new MethodInvoker(delegate { advTreeAssets.Enabled = false; }));

            Kml = ServiceProxy.UtilityService.GetXmissionAssetKML(DataKeyId, CurrentSearchType, CurrentSearchText);
        }

        public override void AsychGetKmlComplete(object sender, RunWorkerCompletedEventArgs e)
        {
            base.AsychGetKmlComplete(sender, e);
            advTreeAssets.Enabled = true;

            if (e.Cancelled)
            {
                // not sure what to do
            }

            if (e.Error != null)
            {
                DevComponents.DotNetBar.MessageBoxEx.Show("Error retrieving map data.",
                    "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            ShowKmlInMap();
        }

        #endregion

        #region Context Menu / Main Bar (Implements IGridBase)

        public override void DoSearch(Common.Enumerations.GridSearchByTpes searchBy, string searchBytext)
        {

        }

        public override void DoExportToExcel()
        {
            //just call method in base
            base.DoExportToExcel();
        }

        public override void DoEdit()
        {
            EntityLibrary.UtilityAssetXMissionStructuresPoleEntity _Asset;
            _Asset = advTreeAssets.GetSelectedEntity() as EntityLibrary.UtilityAssetXMissionStructuresPoleEntity;

            Editors.EditXmissionAsset _Editor = new Editors.EditXmissionAsset(true, _Asset);
            if (_Editor.ShowDialog() == DialogResult.OK)
            {
                //update the record in the grid

            }
            _Editor.Dispose();
        }

        public override void DoOpen()
        {
            EntityLibrary.UtilityAssetXMissionStructuresPoleEntity _Asset;
            _Asset = advTreeAssets.GetSelectedEntity() as EntityLibrary.UtilityAssetXMissionStructuresPoleEntity;

            Editors.EditXmissionAsset _Editor = new Editors.EditXmissionAsset(false, _Asset);
            _Editor.ShowDialog();
            _Editor.Dispose();
        }

        public override void DoMapForGrid()
        {
            StartAsynchGetKml();
        }

        public override void DoMapForRecord()
        {

        }

        public override bool ContextShowAllowed()
        {
            EntityLibrary.UtilityAssetXMissionStructuresPoleEntity _Asset;
            _Asset = advTreeAssets.GetSelectedEntity() as EntityLibrary.UtilityAssetXMissionStructuresPoleEntity;

            if (_Asset == null)
                return false;

            SelectedRowLatitude = _Asset.Latitude;
            SelectedRowLongitude = _Asset.Longitude;

            return true;
        }

        #endregion

        public void ShowData(int utilityId)
        {
            DataKeyId = utilityId;
            BindCombos();
            DataDescription = "Transmission Assets";
            advTreeAssets.Visible = false;
            StartAsynchLoad();
        }

    }
}
